package top.houry.im.server.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ImServerConnectionUtils {

    private static final Map<String, ChannelHandlerContext> ON_LINE_CHANNEL_HANDLER_CONTEXT_MAP = new ConcurrentHashMap<>(256);





    public static void online(String userId, ChannelHandlerContext ctx) {
        if (!ctx.channel().isActive() || ObjectUtil.isNull(userId)) {
            return;
        }
        ON_LINE_CHANNEL_HANDLER_CONTEXT_MAP.put(userId, ctx);
        AttributeKey<String> key = AttributeKey.valueOf("user");
        ctx.channel().attr(key).set(userId);
    }

    public static ChannelHandlerContext getCtx(String userId) {
        return ON_LINE_CHANNEL_HANDLER_CONTEXT_MAP.get(userId);
    }



}
